<template>
  <div class="todo-header">
    <input type="text" v-model.trim="content" @keydown.enter="addTask"
           placeholder="请输入你的任务名称，按回车键确认"/>
  </div>
</template>

<script>
  export default {
    name: "v-header",
    data(){
      return {
        content:""
      }
    },
    methods:{
      addTask(){
        //看门狗
        if(!this.content){return}
        //获取最新的id
        let id = localStorage.getItem("todo-id") || 0;
        id = +id;
        //构建任务
        let task = {id:id++,checked:false,content:this.content}
        //将任务传给app组件 新增任务列表
        this.$bus.$emit("addTask",task)
        //将id重新存入到localStorage中
        localStorage.setItem("todo-id",id);
        //清空输入框
        this.content=""
      }
    }
  }
</script>

<style scoped>
  .todo-header input {
    width: 560px;
    height: 28px;
    font-size: 14px;
    border: 1px solid #ccc;
    border-radius: 4px;
    padding: 4px 7px;
  }

  .todo-header input:focus {
    outline: none;
    border-color: rgba(82, 168, 236, 0.8);
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
  }
</style>